package com.aisile.crowd_funding.system.controller;

import com.aisile.crowd_funding.common.utils.R;
import com.aisile.crowd_funding.common.vo.Tree;
import com.aisile.crowd_funding.system.pojo.SysMenu;
import com.aisile.crowd_funding.system.pojo.SysMenuExample;
import com.aisile.crowd_funding.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @标题 MenuController
 * @作者 Mr.chen
 * @创建日期 2018/12/6 13:54
 * @版本号 1.0
 **/
@Controller
@RequestMapping("sys/menu")
public class MenuController {

    public static final String VIEW_PATH = "system/menu/";

    @Autowired
    MenuService menuService;
    /**
     * 跳转菜单页面
     * @return
     */
    @RequestMapping
    public String menu(){
        return VIEW_PATH + "menu";
    }
    /**
     * 查询 菜单列表
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public List<SysMenu> list(){
        return menuService.selectByExample(null);
    }

    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping("add")
    public String add(@RequestParam(value = "parentId",defaultValue = "0") Long parentId,Model model){
        if(parentId==0){
            model.addAttribute("pName","根目录");
        }else {
            SysMenu sysMenu = menuService.selectByPrimaryKey(parentId);
            model.addAttribute("pName",sysMenu.getName());
        }
        model.addAttribute("parentId",parentId);
        return VIEW_PATH + "add";
    }

    /**
     * 保存菜单
     * @param sysMenu
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public R save(SysMenu sysMenu){
        int insert = menuService.insert(sysMenu);
        if(insert>0){
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("edit")
    public String edit(Long menuId,Model model){
        SysMenu sysMenu = menuService.selectByPrimaryKey(menuId);
        //父节点
        if(sysMenu.getParentId()==0){
            model.addAttribute("pName","根目录");
        }else {
            SysMenu parent = menuService.selectByPrimaryKey(sysMenu.getParentId());
            model.addAttribute("pName",sysMenu.getName());
        }
        model.addAttribute("menu",sysMenu);
        return VIEW_PATH+"edit";
    }

    /**
     * 修改菜单
     * @param sysMenu
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public R update(SysMenu sysMenu){
        int i = menuService.updateByPrimaryKey(sysMenu);
        if(i>0){
             return R.ok();
        }
        return R.error();
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @RequestMapping("del")
    @ResponseBody
    public R del(Long menuId){
        int i = menuService.selectChildCount(menuId);
        if(i>0){
            //有子节点
            return R.error("请先删除子节点");
        }else {
            int i1 = menuService.deleteByPrimaryKey(menuId);
            if(i1>0){
                return R.ok();
            }
        }
        return R.error();
    }

    /**
     * 查询树的数据
     * @return
     */
    @RequestMapping("tree")
    @ResponseBody
    public Tree getTree(){
        return menuService.getTree();
    }

    /**
     * 根据角色ID 生成的树
     * @param roleId
     * @return
     */
    @RequestMapping("tree/{roleId}")
    @ResponseBody
    public Tree getTreeByRoleId(@PathVariable Long roleId){
        return menuService.getTree(roleId);
    }
}
